﻿Imports VendasDAL
Public Class OrdemServicoBLL
    Dim ObjDAL As OrdemServicoDAL

    Property OrdCodigo As Integer
    Property ProCodigo As Integer
    Property DataInicial As Nullable(Of DateTime)
    Property DataFinal As Nullable(Of DateTime)
    Property Qtde As Integer
    Property ValorUnitario As Decimal
    Property Desconto As Nullable(Of Decimal)
    Property Total As Nullable(Of Decimal)
    Property CodFuncionario As Integer
    Property CodPlaca As Integer
    Property CodStatus As Integer


#Region "Construtor"
    Sub New()

    End Sub

    Sub New(codigo As Integer)
        carrega(codigo)
    End Sub

    Sub New(codigo As Integer, procodigo As Integer)
        Me.OrdCodigo = codigo
        Me.ProCodigo = procodigo
        Me.Qtde = 0
        Me.Desconto = 0.0
        Me.Total = 0.0
        Me.CodStatus = 0
        Me.ValorUnitario = 0.0
    End Sub

    Sub New(procodigo As Integer, qtde As Integer, vlunitario As Decimal, desconto As Decimal, ordcodigo As Integer, total As Decimal, codstatus As Integer)
        Me.OrdCodigo = ordcodigo
        Me.ValorUnitario = vlunitario
        Me.Desconto = desconto
        Me.Qtde = qtde
        Me.Desconto = desconto
        Me.ProCodigo = procodigo
        Me.Total = total
        Me.CodStatus = codstatus
    End Sub

    Sub New(Codigo As Integer, PlaCodigo As Integer, CodStatus As Integer, CodFuncionario As Integer, DataInicial As Nullable(Of DateTime), DataFinal As Nullable(Of DateTime), Total As Nullable(Of Decimal), Desconto As Nullable(Of Decimal))
        Me.OrdCodigo = Codigo
        Me.CodPlaca = PlaCodigo
        Me.CodStatus = CodStatus
        Me.CodFuncionario = CodFuncionario
        Me.DataInicial = DataInicial
        Me.DataFinal = DataFinal
        Me.Total = Total
        Me.Desconto = Desconto
    End Sub
#End Region

#Region "Métodos"
    Public Function RetItens(ColCodigo As Integer) As DataTable
        Try
            ObjDAL = New OrdemServicoDAL
            Return ObjDAL.RetItens(ColCodigo)
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Sub RetItem()
        Dim tab As DataTable
        Try
            ObjDAL = New OrdemServicoDAL
            tab = ObjDAL.RetItem(OrdCodigo, ProCodigo)
            If (tab.Rows.Count > 0) Then
                Me.ProCodigo = Integer.Parse(tab.Rows(0)("pro_codigo").ToString)
                Me.Qtde = Integer.Parse(tab.Rows(0)("ito_qtde").ToString)
                Me.ValorUnitario = Decimal.Parse(tab.Rows(0)("ito_valorunitario").ToString)
                Me.Desconto = tab.Rows(0)("ito_desconto").ToString
                Me.CodStatus = tab.Rows(0)("sta_codigo").ToString
            End If

        Catch ex As Exception
            Throw
        End Try
    End Sub

    Public Function IAEOrdem(Operacao As Char) As Integer
        Try
            ObjDAL = New OrdemServicoDAL
            Return ObjDAL.IAEOrdem(OrdCodigo, Total, DataInicial, DataFinal, CodFuncionario, CodPlaca, CodStatus, Desconto, Operacao)
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function InsereAgendamento(Codigo As Integer) As Integer
        Try
            ObjDAL = New OrdemServicoDAL
            Return ObjDAL.InsereAgendamento(Codigo)
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Sub IAEItens(Operacao As Char)
        Try
            ObjDAL = New OrdemServicoDAL
            ObjDAL.IAEItens(OrdCodigo, ProCodigo, Qtde, ValorUnitario, Desconto, Total, CodStatus, Operacao)
        Catch ex As Exception
            Throw
        End Try
    End Sub

#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            ObjDAL = New OrdemServicoDAL
            ret = ObjDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function

    Function locdata(ByVal dataIni As DateTime, ByRef datafim As DateTime) As DataTable 'buscar datas cbfiltro
        Dim ret As DataTable
        Try
            ObjDAL = New OrdemServicoDAL
            If dataIni <= datafim Then
                ret = ObjDAL.locadata(dataIni, datafim)
            Else : Throw New Exception("Data inical maior que data final")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            ObjDAL = New OrdemServicoDAL
            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.CodFuncionario = Integer.Parse(tab.Rows(0)("fun_codigo").ToString)
                Me.CodPlaca = tab.Rows(0)("pla_codigo").ToString
                Me.OrdCodigo = tab.Rows(0)("ord_codigo").ToString

                If (tab.Rows(0)("ord_datainicial") Is DBNull.Value) Then : Me.DataInicial = Nothing
                Else : Me.DataInicial = tab.Rows(0)("ord_datainicial").ToString
                End If

                If (tab.Rows(0)("ord_datafinal") Is DBNull.Value) Then : Me.DataFinal = Nothing
                Else : Me.DataFinal = tab.Rows(0)("ord_datafinal").ToString
                End If

                If (tab.Rows(0)("ord_total") Is DBNull.Value) Then : Me.Total = Nothing
                Else : Me.Total = tab.Rows(0)("ord_total").ToString
                End If
                Me.CodStatus = tab.Rows(0)("sta_codigo").ToString

            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

    Sub Limpar()

        Me.ProCodigo = Nothing
        Me.DataInicial = Nothing
        Me.DataFinal = Nothing
        Me.Qtde = Nothing
        Me.ValorUnitario = Nothing
        Me.Desconto = Nothing
        Me.CodFuncionario = Nothing
        Me.CodPlaca = Nothing
        Me.OrdCodigo = Nothing
        Me.DataInicial = Nothing
        Me.DataFinal = Nothing
        Me.CodStatus = Nothing
        Me.Total = Nothing
    End Sub
End Class
